package com.example.androiddemo;

import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.example.ui.base.BaseFragment;

public class SpanStrFragment extends BaseFragment implements OnClickListener{
	
	private TextView spanTv;

	@Override
	public int getLayoutId() {
		return R.layout.span_str_frg;
	}

	@Override
	public void initialViews(View root, LayoutInflater inflater) {
		spanTv = (TextView) root.findViewById(R.id.spanTv);
		
		root.findViewById(R.id.foreColor).setOnClickListener(this);
		root.findViewById(R.id.backColor).setOnClickListener(this);
		
//		span();
//		underLineSpan();
//		imageSpan();
		foreColorSpan();
	}
	
	private void span() {
		SpannableString spannableString = new SpannableString("打字");
		StyleSpan span = new StyleSpan(Typeface.BOLD_ITALIC);
		spannableString.setSpan(span, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		spanTv.append(spannableString);
	}
	
	private void underLineSpan() {
		SpannableString spannableString = new SpannableString("哈哈fwef");
		UnderlineSpan span = new UnderlineSpan();
		spannableString.setSpan(span, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		spanTv.setText(spannableString);
	}
	
	private void imageSpan() {
		SpannableString spannableString = new SpannableString("sd就哈哈");
		Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
		d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
		
		ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
		spannableString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		spanTv.setText(spannableString);
	}
	
	private void foreColorSpan() {
		SpannableString sb = new SpannableString("第一段aaabb");
		ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.GREEN);
		
		sb.setSpan(foregroundColorSpan, 1, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		spanTv.setText(sb);
	}
	
	private void backColorSpan() {
		SpannableString sb = new SpannableString("第一段aaabb");
		BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.BLUE);
		
		sb.setSpan(backgroundColorSpan, 2, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		spanTv.setText(sb);
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.foreColor:
			foreColorSpan();
			break;
			
		case R.id.backColor:
			backColorSpan();
			break;

		default:
			break;
		}
	}

}
